﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Configuration;
using System.Web.Configuration;
using System.IO;
using System.Text;
using Frame.Lib.Core.User;
using Frame.Lib.Data;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;
using Frame.Lib.Common;
using Frame.Lib.Core.Tool;

namespace Frame.Web.Admin.Tools
{
    public partial class ConnectionString : System.Web.UI.Page
    {
        private UserBO Users
        {
            get
            {
                return UserBO.Instance<UserBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        protected void btnEncrypt_Click(object sender, EventArgs e)
        {
            string src = txtSrc.Text.Trim();

            Type type = typeof(ConnectionStringManager);

            MethodInfo mInfo = type.GetMethod("Encrypt", System.Reflection.BindingFlags.Static| BindingFlags.NonPublic);

            object[] objs = new object[] { src };

            string dest = mInfo.Invoke(null, objs) as string;

            txtDest.Text = dest;

        }

        protected void btnDecrypt_Click(object sender, EventArgs e)
        {
            string src = txtSrc.Text.Trim();

            Type type = typeof(ConnectionStringManager);

            MethodInfo mInfo = type.GetMethod("Decrypt", System.Reflection.BindingFlags.Static | BindingFlags.NonPublic);

            object[] objs = new object[] { src };

            string dest = mInfo.Invoke(null, objs) as string;

            txtDest.Text = dest;
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //   // System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString = TextBox1.Text;

        //    Configuration config = WebConfigurationManager.OpenWebConfiguration("/config/Database.config");

          
        //   // System.Web.Configuration.WebConfigurationManager.
        //    config.ConnectionStrings.ConnectionStrings.Remove("Default");
        //    ConnectionStringSettings setting = new ConnectionStringSettings();
        //    setting.Name = "Default";
        //    setting.ConnectionString = TextBox1.Text;

        //    config.ConnectionStrings.ConnectionStrings.Add(setting);

        //    config.Save();
        //  //  config.SaveAs(Server.MapPath("/config").TrimEnd('\\') + "\\test.config");

        //    //if (isModified)
        //    //{
        //    //    config.AppSettings.Settings.Remove("box");
        //    //}
        //    //config.AppSettings.Settings.Add("box", txt_value.Text);
        //    //config.Save(ConfigurationSaveMode.Modified);
        //    ConfigurationManager.RefreshSection("connectionStrings");


        //    JSUtil.Alert(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);

        //}

        protected void btnView_Click(object sender, EventArgs e)
        {

            if (Validate2())
            {
                using (FileStream fs = new FileStream(Server.MapPath(GlobalVar.WebAppPath + "config/database.config"), FileMode.Open))
                {
                   
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        txtConnectionString.Text = sr.ReadToEnd();
                    }
                }
            }
        }

        private bool Validate2()
        {
            string loginName = txtLoginName.Text.Trim();
            string password = txtPassword.Text.Trim();

            if (AdminTool.Validate(loginName, password))
            {
                return true;
            }
            else
            {
                JSUtil.Alert("用户名或密码错");
                return false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Validate2())
            {
                using (FileStream fs = new FileStream(Server.MapPath(GlobalVar.WebAppPath + "config/database.config"), FileMode.Truncate))
                {
                    //byte[] byts = System.Text.Encoding.UTF8.GetBytes(txtConnectionString.Text.Trim());
                    //fs.Write(byts, 0, byts.Length);
                    //fs.Flush();
                    //fs.Close();

                    //有问题
                    using (StreamWriter sr = new StreamWriter(fs, Encoding.UTF8))
                    {
                        string str = txtConnectionString.Text.Trim().Replace("\r\n", "\n");
                        sr.Write(str);
                        sr.Flush();
                        fs.Flush();
                        JSUtil.Alert("保存成功");
                    }
                }
            }
        }
    }
}